package com.hotel.user.mapper;

import com.hotel.common.entity.Role;
import com.hotel.common.utils.DBTools;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository
public class RoleMapper {

    public Role findById(Long id) {
        String sql = "SELECT id, role_name, role_code, description, created_at FROM role WHERE id = ?";
        List<Map<String, String>> result = DBTools.query(sql, id);
        if (result.isEmpty()) {
            return null;
        }
        return mapToRole(result.get(0));
    }

    public Role findByRoleCode(String roleCode) {
        String sql = "SELECT id, role_name, role_code, description, created_at FROM role WHERE role_code = ?";
        List<Map<String, String>> result = DBTools.query(sql, roleCode);
        if (result.isEmpty()) {
            return null;
        }
        return mapToRole(result.get(0));
    }

    public List<Role> findAll() {
        String sql = "SELECT id, role_name, role_code, description, created_at FROM role ORDER BY created_at DESC";
        List<Map<String, String>> result = DBTools.query(sql);
        return result.stream().map(this::mapToRole).collect(Collectors.toList());
    }

    public Long insert(Role role) {
        String sql = "INSERT INTO role (role_name, role_code, description, created_at) VALUES (?, ?, ?, ?)";
        LocalDateTime now = LocalDateTime.now();
        DBTools.update(sql, role.getRoleName(), role.getRoleCode(), role.getDescription(), now);
        return Long.valueOf(DBTools.querySingle("SELECT LAST_INSERT_ID()"));
    }

    public int update(Role role) {
        String sql = "UPDATE role SET role_name = ?, role_code = ?, description = ? WHERE id = ?";
        return DBTools.update(sql, role.getRoleName(), role.getRoleCode(), role.getDescription(), role.getId());
    }

    public int deleteById(Long id) {
        String sql = "DELETE FROM role WHERE id = ?";
        return DBTools.update(sql, id);
    }

    public List<Role> findByUserId(Long userId) {
        String sql = "SELECT r.id, r.role_name, r.role_code, r.description, r.created_at " +
                    "FROM role r INNER JOIN user_role ur ON r.id = ur.role_id WHERE ur.user_id = ?";
        List<Map<String, String>> result = DBTools.query(sql, userId);
        return result.stream().map(this::mapToRole).collect(Collectors.toList());
    }

    public List<Role> findAllWithPermissions() {
        String sql = "SELECT DISTINCT r.id, r.role_name, r.role_code, r.description, r.created_at " +
                    "FROM role r INNER JOIN role_permission rp ON r.id = rp.role_id " +
                    "ORDER BY r.created_at DESC";
        List<Map<String, String>> result = DBTools.query(sql);
        return result.stream().map(this::mapToRole).collect(Collectors.toList());
    }

    private Role mapToRole(Map<String, String> map) {
        return Role.builder()
                .id(Long.valueOf(map.get("id")))
                .roleName(map.get("role_name"))
                .roleCode(map.get("role_code"))
                .description(map.get("description"))
                .createdAt(LocalDateTime.parse(map.get("created_at").replace(" ", "T")))
                .build();
    }
}